package com.shop.controller;

/**
 * 活动增删改查，
 * 活动秒杀商品的限购，个人超过限购数量/活动结束返回错误信息
 */

import com.shop.dao.ActivityDao;
import com.shop.entity.Activity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/activities")
public class ActivityController {

    @Autowired
    private ActivityDao activityDao;

    // 获取所有活动
    @GetMapping
    public List<Activity> getAllActivities() {
        return activityDao.getAllActivities();
    }

    // 根据ID获取活动
    @GetMapping("/{id}")
    public ResponseEntity<Activity> getActivity(@PathVariable int id) {
        Activity activity = activityDao.getActivityById(id);
        return activity != null ? ResponseEntity.ok(activity) : ResponseEntity.notFound().build();
    }

    // 根据活动名称获取活动
    @GetMapping("/name/{name}")
    public List<Activity> getActivitiesByName(@PathVariable String name) {
        return activityDao.getActivitiesByName(name);
    }

    // 添加活动
    @PostMapping
    public ResponseEntity<Activity> addActivity(@RequestBody Activity activity) {
        activityDao.insertActivity(activity);
        return new ResponseEntity<>(activity, HttpStatus.CREATED);
    }

    // 更新活动
    @PutMapping("/{id}")
    public ResponseEntity<Activity> updateActivity(@PathVariable int id, @RequestBody Activity activityDetails) {
        activityDetails.setId(id);
        activityDao.updateActivity(activityDetails);
        return ResponseEntity.ok(activityDetails);
    }

    // 删除活动
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteActivity(@PathVariable int id) {
        activityDao.deleteActivity(id);
        return ResponseEntity.noContent().build();
    }
}
